# Scene viewer widget
#

import sys, os, os.path, time, traceback, math
pj = os.path.join

from PyQt4 import QtGui, QtCore, QtOpenGL

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import osg2


class SceneViewWidget(QtOpenGL.QGLWidget):
   """ Widget to view a scene. 
   
   @var mManager: OpenSG SSM we are using locally
   @var mScene: The scene we are hooked up to view
   """   
   def __init__(self, scene, parent = None):
      QtOpenGL.QGLWidget.__init__(self, parent)
      
      self.mManager = osg2.SimpleSceneManager()
      
      self.mPwin = osg2.PassiveWindow.create()
      self.mManager.setWindow(self.mPwin)
      self.mManager.setStatistics(True)
      self.mManager.setHeadlight(True)
      self.mManager.setUseTraversalAction(True)
      
      self.setScene(scene)

      self.mTimer = QtCore.QTimer(self)
      self.connect(self.mTimer, QtCore.SIGNAL("timeout()"), self.updateGL)
      self.mTimer.start(0)

   def setScene(self, scene):
      self.mScene = scene
      self.setWindowTitle("%s: %s"%(scene.mName,scene.mFileName))
      
      if self.mScene:
         self.mManager.setRoot(scene.mSceneRoot)
         self.mManager.showAll()
      else:
         self.mManager.setRoot(None)

   def getManager(self):
      return self.mManager

   def showAll(self):
      self.mManager.showAll()
    
   def minimumSizeHint(self):
      return QtCore.QSize(50, 50)
    
   def sizeHint(self):
      return QtCore.QSize(400, 400)
    
   def initializeGL(self):
      glClearColor(0,0,0,0)
      glEnable(GL_DEPTH_TEST)
      glEnable(GL_CULL_FACE)
      #glShadeModel(GL_FLAT)
    
   def paintGL(self):
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
      self.mManager.idle()
      self.mManager.redraw()

   def resizeGL(self, width, height):
      self.mManager.resize(width,height)
      glViewport(width/2,0,width,height)
    
   def mousePressEvent(self, event):
      print "OSGQGLManagedWidget::mousePressEvent()"
      if event.button() == QtCore.Qt.LeftButton:
         self.mManager.mouseButtonPress(osg2.SimpleSceneManager.MouseLeft, event.x(), event.y())
      elif event.button() == QtCore.Qt.MidButton:
         self.mManager.mouseButtonPress(osg2.SimpleSceneManager.MouseMiddle, event.x(), event.y())
      elif event.button() == QtCore.Qt.RightButton:
         self.mManager.mouseButtonPress(osg2.SimpleSceneManager.MouseRight, event.x(), event.y())
      #self.paintGL()
      self.updateGL()

   def mouseReleaseEvent(self, event):
      print "OSGQGLManagedWidget::mouseReleaseEvent()"
      if event.button() == QtCore.Qt.LeftButton:
         self.mManager.mouseButtonRelease(osg2.SimpleSceneManager.MouseLeft, event.x(), event.y())
      elif event.button() == QtCore.Qt.MidButton:
         self.mManager.mouseButtonRelease(osg2.SimpleSceneManager.MouseMiddle, event.x(), event.y())
      elif event.button() == QtCore.Qt.RightButton:
         self.mManager.mouseButtonRelease(osg2.SimpleSceneManager.MouseRight, event.x(), event.y())
      #self.paintGL()
      self.updateGL()
    
   def mouseMoveEvent(self, event):
      #print "Mouse move: ", (event.x(), event.y())
      self.mManager.mouseMove(event.x(), event.y())
      #self.paintGL()
      self.updateGL()

   def wheelEvent (self, we):
      print "OSGQGLManagedWidget::wheelEvent()"

      if we.delta() > 0:
         button = osg2.SimpleSceneManager.MouseUp;
      else:
         button = osg2.SimpleSceneManager.MouseDown;

      self.mManager.mouseButtonPress(button, we.x(), we.y())

      #self.paintGL()
      self.updateGL()
